Find the first repeated wordΒΆ
Find the first repeated word in a given string.
def first_repeated_word(S):
temp = set()
for word in S.split():
if word in temp:
return word
else:
temp.add(word)
return 'None'
# test
print(first_repeated_word("ab ca bc ab")) # ab
print(first_repeated_word("ab ca bc ab ca ab bc")) # ab
print(first_repeated_word("ab ca bc ca ab bc")) # ca
print(first_repeated_word("ab ca bc")) # None